package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.limao.DBDataClass.UserAddressDataClass;
import com.limao.DbConnection.DbConnectionManager;

public class UserAddressProvider {
	private static class DB_user_address_ProviderContainer {
		private static UserAddressProvider instance = new UserAddressProvider();
	}

	public static UserAddressProvider getInstance() {
		return DB_user_address_ProviderContainer.instance;
	}

	private static final String table = "cd_user_address";

	// 查询 by id 一个用户最多有几个地址有前台控制。。。。
	private static final String cd_user_address_LOAD = "SELECT * FROM " + table
			+ " WHERE id=? order by isdef DESC ,time DESC ";

	// 查询 by id 一个用户最多有几个地址有前台控制。。。。
	private static final String cd_user_address_LOAD_ALL = "SELECT * FROM "
			+ table + " WHERE userid=? order by isdef DESC ,time DESC limit 1 ";

	// 修改资料
	private static final String cd_user_address_UPDATE = "UPDATE "
			+ table
			+ " SET userid=?,person_name=?,tel=?,shengid=?,cityid=?,address=?,isdef=?,time=? WHERE id=?";
	

	private static final String cd_user_address_UPDATE_time = "UPDATE " + table
			+ " SET  time=? WHERE id=?";

	private static final String cd_user_address_insert = "INSERT INTO  "
			+ table
			+ "(id,userid,person_name,tel,shengid,cityid,address,isdef,time) VALUES (?,?,?,?,?,?,?,?,?)";

	// 初始化
	private UserAddressProvider() {

	}

	//新鲜宝   查询 by id
	public UserAddressDataClass load_User_Address_UserDef(int userid) {
		UserAddressDataClass tmp = new UserAddressDataClass();;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement(cd_user_address_LOAD_ALL);
			pstmt.setInt(1, userid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp.id = rs.getInt("id") + "";
				tmp.userid = rs.getInt("userid") + "";
				tmp.person_name = rs.getString("person_name");
				tmp.tel = rs.getString("tel");
				
				tmp.shengid = rs.getInt("shengid") + "";
				tmp.cityid = rs.getInt("cityid") + "";
				tmp.address = rs.getString("address");
				tmp.isdef = rs.getInt("isdef") + "";
				tmp.time = rs.getLong("time") + "";
			}
		} catch (Exception e) {
			tmp = null;
			e.printStackTrace();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}



}
